package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Annunciate;
import com.qf.entity.Logs;
import com.qf.entity.User;
import com.qf.mapper.AnnounceDao;
import com.qf.mapper.LogsDao;
import com.qf.service.AnnounceService;
import com.qf.service.LogsService;
import com.qf.task.OATask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

import com.qf.mapper.AnnounceMapper;


/**
 * 2019/12/10
 * qjh
 * springboot_oa_five
 * 面向对象面向君 不负代码不负卿
 */

/**
 * 通告管理
 */
@Service
public class AnnounceServiceImpl implements AnnounceService {
    @Autowired
    private AnnounceDao announceDao;
    @Autowired
    private LogsService logsService;

    @Autowired
    private AnnounceMapper announceMapper;

    @Override
    public PageInfo<Annunciate> findAllAnnounce(Integer page, String annName, String publishTimeBegin, String publishTimeOver) {
        PageHelper.startPage(page, 5);
        Map map = new HashMap();
        map.put("annName",annName);
        map.put("publishTimeBegin",publishTimeBegin);
        map.put("publishTimeOver",publishTimeOver);
        List<Annunciate> annunciate = announceDao.findAllAnnunciate(map);
        PageInfo<Annunciate> pageInfo = new PageInfo<>(annunciate);
        return pageInfo;
    }

    @Override
    @Transactional
    public Integer updataStatus(Integer anId, Integer status, HttpSession session) {
        Date dates = new Date();
        Annunciate annunciate = announceDao.findAnnunciate(anId);
        Date endTime = annunciate.getEndTime();
        Date startTime = annunciate.getStartTime();
        if(dates.getTime()<startTime.getTime()||dates.getTime()>endTime.getTime()){
            return 0;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = formatter.format(dates);
        System.out.println(date);
        Map map = new HashMap();
        map.put("anId",anId);
        map.put("status",status);
        map.put("publishTime", date);
        Integer integer = announceDao.updataStatus(map);

        //记录日志
        User user = (User) session.getAttribute("user");
        logsService.addAnnunciate(user,annunciate);

        return integer;
    }

    @Override
    @Transactional
    public Integer bathDelete(int[] arr) {


        //查询已发布的通告
        List<Annunciate> in = announceDao.findIn(arr);

        List list = new ArrayList();
        for (int ar : arr) {
            boolean bool =true;
            for (Annunciate ins : in) {
                if(ar==ins.getAnId()){
                    bool=false;
                    break;
                }
            }
            if(bool){
                list.add(ar);
            }
        }
        if(list.size()!=0){
            Integer integer = announceDao.bathDelete(list);
            return integer;
        }else{
            return 0;
        }
//        Integer integer = announceDao.bathDelete(arr);
//        return integer;
    }

    @Override
    @Transactional
    public Integer addAnnunciate(Annunciate annunciate) {
        Date startTime = annunciate.getStartTime();
        Date endTime = annunciate.getEndTime();
        Date date = new Date();
        if(date.getTime()<startTime.getTime()){
            annunciate.setStatus(0);
        }else if(date.getTime()>endTime.getTime()){
            annunciate.setStatus(-1);
        }else{
            annunciate.setStatus(1);
        }
        Integer integer = announceDao.addAnnunciate(annunciate);
        return integer;
    }

    @Override
    public Annunciate findAnnunciate(Integer anId) {
        Annunciate annunciate = announceDao.findAnnunciate(anId);
        return annunciate;
    }

    @Override
    @Transactional
    public Integer updateAnnunciate(Annunciate annunciate) {
        Date startTime = annunciate.getStartTime();
        Date endTime = annunciate.getEndTime();
        Date date = new Date();
        if(date.getTime()<startTime.getTime()){
            annunciate.setStatus(0);
        }else if(date.getTime()>endTime.getTime()){
            annunciate.setStatus(-1);
        }else{
            annunciate.setStatus(1);
        }
        Integer integer = announceDao.updateAnnunciate(annunciate);
        return integer;
    }

    @Override
    public List<Annunciate> findAnnunciateByArr(int[] arr) {
        List<Annunciate> annunciateByArr = announceDao.findAnnunciateByArr(arr);
        return annunciateByArr;
    }
    @Override
    public PageInfo<Annunciate> findAllInfoByStatus(Integer pageNum, Integer size, String annName,String startTimes, String endTimes) {
        PageHelper.startPage(pageNum,size);
        List<Annunciate> annunciateList = announceMapper.findAllInfoByStatus(annName,startTimes,endTimes);
        PageInfo<Annunciate> pageInfo = new PageInfo<>(annunciateList);
        return pageInfo;
    }

    @Override
    public List<Annunciate> getExcle(Integer[] anIds) {
        return announceMapper.findInfoByAnIds(anIds);
    }

    @Override
    public Annunciate findInfoByAnId(Integer anId) {
        Annunciate param = new Annunciate();
        param.setAnId(anId);
        return announceMapper.selectByPrimaryKey(param);
    }
}
